package com.example.linyiquan.login.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.linyiquan.login.MyVolley;
import com.example.linyiquan.login.R;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    //版本1
    private EditText et_username;
    private EditText et_password;
    private TextView remember_password;
    private TextView tv_register;
    private CheckBox cb_password;
    private Button bt_login;
    private SharedPreferences.Editor spe;
    private SharedPreferences sharedPreferences;
    private String username;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        sharedPreferences=LoginActivity.this.getSharedPreferences("data",MODE_PRIVATE);

        initView();

    }

    private void initView(){
        et_username= (EditText) findViewById(R.id.et_username);
        et_username.setText(sharedPreferences.getString("username",""));
        et_password= (EditText) findViewById(R.id.et_password);
        et_password.setText(sharedPreferences.getString("password",""));
        remember_password=(TextView)findViewById(R.id.tv_remember_password);
        tv_register= (TextView) findViewById(R.id.tv_register);
        tv_register.setOnClickListener(this);
        bt_login=(Button)findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
        cb_password= (CheckBox) findViewById(R.id.cb_password);
        if (sharedPreferences.getBoolean("check",false)){
            {
                et_username.setText(sharedPreferences.getString("username",username));
                et_password.setText(sharedPreferences.getString("password",password));
                cb_password.setChecked(true);
            }

        }


    }

    @Override
    public void onClick(View v) {


        switch (v.getId()){
            case R.id.bt_login:

                username = et_username.getText().toString();
                password = et_password.getText().toString();

                if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                    Toast.makeText(this, "帐号密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                boolean checkboxlogin=cb_password.isChecked();

                if (checkboxlogin){
                    spe=sharedPreferences.edit();
                    spe.putString("usernamae",username);
                    spe.putString("password",password);
                    spe.putBoolean("check",true);
                    spe.commit();
                }else {
                    spe=sharedPreferences.edit();
                    spe.putString("usernamae",username);
                    spe.putString("password",null);
                    spe.putBoolean("check",false);
                    spe.commit();
                }

                LoginToServer(username,password);
                spe.putString("username",username);
                spe.commit();

                Intent user=new Intent(LoginActivity.this,GridActivity.class);
                startActivity(user);

                break;

            case R.id.tv_register:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;

        }

    }

    public void LoginToServer(String username,String password){
        MyVolley.init(this);


        String url="http://120.76.96.34/weizhanjsp/user/login?clientType=1&mac=sdfsss&cards=true";

        url+="&username="+username;
        url+="&password="+password;
        Log.d("app",url);
        MyVolley.post(url,null, new MyVolley.onResultListener() {
            @Override
            public void done(JSONObject result) {

                int resultCode;
                try {
                    resultCode = result.getInt("resultCode");
                    if(resultCode==0){
                        Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(LoginActivity.this,result.getString("resultString"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void fail(VolleyError error) {

            }
        });
    }

}
